package com.abc.cloud.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.abc.cloud.common.utils.UserHolder;
import com.abc.cloud.dto.im.GroupResp;
import com.abc.cloud.vo.im.AdminUpdateGroupReq;
import com.abc.cloud.vo.im.GroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.abc.cloud.entity.GroupEntity;
import com.abc.cloud.service.GroupService;
import com.abc.cloud.common.utils.PageUtil;
import com.abc.cloud.common.utils.Result;



/**
 * 分组表
 *
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-11-29 13:19:41
 */
@Api(tags = "联系人分组接口")
@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    /**
     * 列表
     */
    @ApiOperation("获取联系人分组列表")
    @GetMapping("/list")
    public Result<List<GroupResp>> list(){
        Long userId = UserHolder.getUser().getId();
        List<GroupResp> page = groupService.listByUserId(userId);

        return Result.success(page, "获取成功");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
		GroupEntity group = groupService.getById(id);

        return Result.success(group, "group");
    }

    /**
     * 保存
     */
    @ApiOperation("保存联系人分组")
    @PostMapping("/save")
    public Result save(@RequestBody GroupVo groupVo){
        Long userId = UserHolder.getUser().getId();
        groupService.saveGroup(userId, groupVo.getName());

        return Result.success(null, "保存成功");
    }

    /**
     * 修改
     */
    @ApiOperation("修改联系人分组")
    @PutMapping("/update")
    public Result update(@RequestBody GroupVo groupVo){
        Long userId = UserHolder.getUser().getId();
        groupService.updateById(userId, groupVo.getId(), groupVo.getName(), groupVo.getStatus());

        return Result.success(null, "操作成功");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		groupService.removeByIds(Arrays.asList(ids));

        return Result.success(null, "");
    }

    @ApiOperation("管理员分页获取联系人分组列表")
    @GetMapping("/admin/list")
    public Result adminGroupList(@RequestParam Map<String, Object> params){
        PageUtil page = groupService.adminGroupList(params);

        return Result.success(page, "获取成功");
    }

    @ApiOperation("管理员修改联系人分组")
    @PutMapping("/admin/update")
    public Result adminUpdateGroup(@RequestBody AdminUpdateGroupReq adminUpdateGroupReq){
        groupService.adminUpdateGroup(adminUpdateGroupReq);

        return Result.success(null, "操作成功");
    }

    @ApiOperation("管理员批量修改聊联系人分组状态")
    @PutMapping("/admin/updateStatus/{status}")
    public Result adminDeleteGroup(@RequestBody List<Long> ids,
                                  @PathVariable("status") Integer status){
        groupService.adminDeleteGroup(ids, status);

        return Result.success(null, "操作成功");
    }

}
